package com.zhongwang.mms.module.purchase.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.PInventory;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.module.overview.dao.OveIndexDao;
import com.zhongwang.mms.module.purchase.dao.PurPInventoryDao;
import com.zhongwang.mms.module.purchase.dao.PurWarInManageDao;
import com.zhongwang.mms.module.purchase.model.PChangeOrderModel;
import com.zhongwang.mms.module.purchase.model.PurInvQueryModel;
import com.zhongwang.mms.module.purchase.model.PurWarQueryModel;
import com.zhongwang.mms.module.purchase.model.VWProject;
import com.zhongwang.mms.module.purchase.service.PurWarQueryService;
import com.zhongwang.mms.module.purchase.service.PurlnvQueryService;
import com.zhongwang.mms.module.system.log.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 库存综合查询
 * @Author: China.zh
 * @Date: 2020/4/6 12:53
 */
@Slf4j
@Controller
@RequestMapping("/purInvQuery")
public class PurlnvQueryController extends BaseController {

    @Autowired
    private PurlnvQueryService purlnvQueryService;
    @Autowired
    private PurWarInManageDao purWarInManageDao;
    @Autowired
    private OveIndexDao oveIndexDao;
    @Autowired
    private PurPInventoryDao purWarQueryService;
    @Autowired
    private LogService logService;;

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/index")
    public String index(Model model) {
        List<TWarehouseInfo> warehouseInfoList =  purWarInManageDao.findWarehouseInfoList();
        model.addAttribute("warehouseInfoList",warehouseInfoList);
        return "purchase/purInvManage/purInvQuery/page";
    }
    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/index/project/{code}")
    public String indexProject(@PathVariable("code") String code,Model model) {
        model.addAttribute("code",code);
        model.addAttribute("mode","pro");
        List<TWarehouseInfo> warehouseInfoList =  purWarInManageDao.findWarehouseInfoList();
        model.addAttribute("warehouseInfoList",warehouseInfoList);
        return "purchase/purInvManage/purInvQuery/page";
    }

    /**
     * 首页外购件库存总金额（含辽阳、营口机械备件库）跳转使用
     *
     * @return 页面路径
     */
    @GetMapping("/index/{type}")
    public String indexForJump(Model model,@PathVariable("type") String type) {
        String totalPrice = null;
        String title="";
        switch (type){
            case "all":
                title ="外购件库存总金额（含辽阳、营口机械备件库）";
                totalPrice = oveIndexDao.getInventoryInStockTotalAmountByPurchaseAndHalf().toString();
                model.addAttribute("mode",null);
                break;
            case "group":
                title ="集团物资部外购件库（库存金额："+oveIndexDao.getInventoryTotalAmountByPurchase().toString()+"万，项目在库金额："+oveIndexDao.getProjectInStockTotalAmountByPurchase().toString()+"万，库存在库金额："+oveIndexDao.getInventoryInStockTotalAmountByPurchase().toString()+"万）";
//                totalPrice = "("+oveIndexDao.getInventoryTotalAmountByPurchase().toString()+","+
//                        oveIndexDao.getProjectInStockTotalAmountByPurchase().toString()+","+
//                        oveIndexDao.getInventoryInStockTotalAmountByPurchase().toString()+")";
                model.addAttribute("mode",null);
                break;
            case "groupByPro":
                title ="集团物资部外购件库项目在库金额";
                totalPrice = oveIndexDao.getProjectInStockTotalAmountByPurchase().toString();
                model.addAttribute("mode","pro");
                break;
            case "groupByInv":
                title ="集团物资部外购件库库存在库金额";
                totalPrice = oveIndexDao.getInventoryInStockTotalAmountByPurchase().toString();
                model.addAttribute("mode","inv");
                break;
        }

            model.addAttribute("totalPrice",totalPrice);
            model.addAttribute("title",title);
        List<TWarehouseInfo> warehouseInfoList =  purWarInManageDao.findWarehouseInfoList();
        model.addAttribute("warehouseInfoList",warehouseInfoList);
        return "purchase/purInvManage/purInvQuery/page";
    }

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/index/query/{code}/{mode}")
    public String indexQuery(Model model, @PathVariable("code") String code,@PathVariable("mode") String mode) {
        List<TWarehouseInfo> warehouseInfoList =  purWarInManageDao.findWarehouseInfoList();
        model.addAttribute("warehouseInfoList",warehouseInfoList);
        model.addAttribute("code", code);
        model.addAttribute("mode", mode);
        return "purchase/purInvManage/purInvQuery/page";
    }

    /**
     * 列表
     *
     * @param param 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/querylist")
    @RequiresPermissions({"purinv:purinvquery:view"})
    public JsonResult list(PurInvQueryModel param) {
        // 记录开始时间（纳秒）
        long startTime = System.nanoTime();
        logService.saveLog("外购件库存综合查询","开始查询！");
        startPage();
        List<PurInvQueryModel> list = purlnvQueryService.findPurInvList(param);
        // 记录结束时间
        long endTime = System.nanoTime();
        // 计算耗时（秒）
        double elapsedSeconds = (endTime - startTime) / 1_000_000_000.0;
        logService.saveLog("外购件库存综合查询","返回查询结果"+list.size()+"条数据，耗时"+elapsedSeconds+"秒！");
        return jsonResult(list);
    }

    /**
     * 列表
     *
     * @param param 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/changeList")
    @RequiresPermissions({"purinv:purinvquery:view"})
    public JsonResult changeList(PurInvQueryModel param) {
        startPage();
        List<PurInvQueryModel> list = purlnvQueryService.findPurChangeListInvList(param);
        return jsonResult(list);
    }

    @PostMapping("listByPurchase")
    @ResponseBody
    /**
     * 获取外购件专用仓库列表
     */
    public JsonResult listByPurchase() {
        return jsonResult(purlnvQueryService.findWarehouseInfoListByPurchase());
    }

    /**
     * 列表 导出
     *
     * @param param 查询条件
     * @return list
     */
    @PostMapping("/exportExcel")
    @ResponseBody
    @RequiresPermissions({"purinv:purinvquery:export"})
    public JsonResult JsonResult(PurInvQueryModel param) throws IOException {
       return purlnvQueryService.exportExcel(param);
    }

    @PostMapping("/findPInventoryChangeOrderList")
    @ResponseBody
    @RequiresPermissions({"purwar:purwarquery:view"})
    /**
     *@FunctionName: findPInventoryChangeOrderList
     *@Description: 查看该库存的串料信息，来自于哪个物料
     *@Author: 徐一贺
     *@CreateDate: 2020/6/13 15:11
     *@Phone: 18241927380
     *@Param: []
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Throw:
     *@Version: v 1.0.0
     */
    public JsonResult findPInventoryChangeOrderList(PChangeOrderModel param) {
        startPage();
        return jsonResult(purlnvQueryService.findPInventoryChangeOrderList(param));
    }
}
